#include "./res/m.h"

//将灯设备的文件描述符定义成全局变量
int fd;
//定义与驱动程序相同的数据格式
int sw_ctrl;
int iu = 0;
char buf[2];
int led_ctrl_flag1;//灯控自动开关
char *rcv_buf11 = NULL;

// 用于控制灯光的闪烁的线程函数
pthread_t id;


void *LED_blink1(void *arg)
{
    char *rcv_buf_cb = (char *)arg;
    // 灯光闪烁模块
    LED_blink(rcv_buf_cb);
    return NULL;
}


static void event_handler_LED(lv_event_t * e)
{
    if(iu == 0)
    {
        // 打开文件
        fd = open("/dev/led_drv",O_RDWR);
        if(fd == -1)
        {
            perror("open");
            return;
        }

        iu = 1;
    }

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) 
    {
        LV_LOG_USER("State: %s\n", lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
    }
        
    // 根据事件类型和计数器的奇偶性来切换灯的状态
    if(code == LV_EVENT_CLICKED)
    {
        if(iu % 2 != 0)
        {
            sw_ctrl = 1;
            printf("ON:%d\n", sw_ctrl);
            // 标志更新
            LED_state_flag = 1; 
        } 
        
        else 
        {
            sw_ctrl = 0;
            printf("OFF:%d\n", sw_ctrl);
            // 标志更新
            LED_state_flag = 0;
        }
        
        iu++;
        printf("iu:%d\n",iu);
        //利用与驱动程序相同的接口，来达到调用驱动程序的接口效果
        //buf[0] 代表灯的亮灭  1亮  0灭
        //buf[1] 代表灯的编号  7   8   9   10
        buf[0] = sw_ctrl;//全部开灯\关灯
        buf[1] = 7; //灯的编号
        write(fd,buf,sizeof(buf));
        buf[1] = 8; //灯的编号
        write(fd,buf,sizeof(buf));
        buf[1] = 9; //灯的编号
        write(fd,buf,sizeof(buf));
        buf[1] = 10; //灯的编号
        int ret = write(fd,buf,sizeof(buf));
        if(ret == -1)
        {
            perror("write error");
            return;
        }

    }
    
    if (led_ctrl_flag1 == 0)
    {
        // 标志更新
        LED_state_flag = 0; 
        
        buf[0] = 0;//7、8号关灯
        buf[1] = 7; //灯的编号
        write(fd,buf,sizeof(buf));
        buf[1] = 8; //灯的编号
        int ret = write(fd,buf,sizeof(buf));
        if(ret == -1)
        {
            perror("write error");
            return;
        } 

        led_ctrl_flag1 = 2;
    }
    
    if (led_ctrl_flag1 == 1)
    {
        // 标志更新
        LED_state_flag = 1; 
        
        buf[0] = 1;//7、8号开灯
        buf[1] = 7; //灯的编号
        write(fd,buf,sizeof(buf));
        buf[1] = 8; //灯的编号
        int ret = write(fd,buf,sizeof(buf));
        if(ret == -1)
        {
            perror("write error");
            return;
        }         

        led_ctrl_flag1 = 2;
    }
    
  
}


// led_ctrl_flag等于1，则自动开灯
void LED_ctrl(lv_obj_t *win, int led_ctrl_flag, char *rcv_buf1)
{   
    rcv_buf11 = rcv_buf1;
    led_ctrl_flag1 = led_ctrl_flag;
	// 打开文件
	fd = open("/dev/led_drv",O_RDWR);
	if(fd == -1)
	{
		perror("open");
		return;
	}


    // 刚开始先把灯关上
    buf[0] = 0;//全部关灯
	buf[1] = 7; //灯的编号
	write(fd,buf,sizeof(buf));
	buf[1] = 8; //灯的编号
	write(fd,buf,sizeof(buf));
	buf[1] = 9; //灯的编号
	write(fd,buf,sizeof(buf));
	buf[1] = 10; //灯的编号
	write(fd,buf,sizeof(buf));

    //创建线程去闪烁灯
	//pthread_create(&id, NULL, &LED_blink1, (void *)rcv_buf1);
    
    // 创建灯控开关
    lv_obj_t *sw = lv_switch_create(win);
    lv_obj_align(sw, LV_ALIGN_TOP_RIGHT, -50, 100);
    lv_obj_set_size(sw, 70, 40);
    lv_obj_add_event_cb(sw, event_handler_LED, LV_EVENT_ALL, NULL);


    // 关闭文件
	close(fd);
    return;
}